import 'package:FlutterReader/router/context_extension.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../reading/reader/read_local.dart';
import '../reading/book_provider.dart';
import '../reading/models/Book.dart';
import 'clickable.dart';

class ShowNovel extends StatelessWidget {
  final bool isEven;
  final int index;
  final VoidCallback? onTap;
  final String title = "这里是文字说明";
  final double heightC;
  final Book book;

  ShowNovel({
    super.key,
    required this.isEven,
    required this.index,
    this.onTap,
    required this.heightC,
    required this.book,
  });



  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    void jumpWithValue(){
      final readPage = context.routeOf<Read>();
      readPage.arguments(book).to(); // 跳转
    }

    void showDetailsDialog(BuildContext context) {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('详情'),
            content: Text('这是详情内容'),
            actions: <Widget>[
              TextButton(
                child: Text('关闭'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        },
      );
    }

    void showDeleteConfirmation(BuildContext context) {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('删除'),
            content: Text('确定要删除吗？'),
            actions: <Widget>[
              TextButton(
                child: Text('取消'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              TextButton(
                child: Text('确定'),
                onPressed: () async {
                  // 在这里处理删除逻辑
                  // 使用 provider 进行删除。
                  final bookProvider = Provider.of<BookProvider>(context, listen: false);
                  await bookProvider.deleteBookAndChapter(book.id ?? -1);
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        },
      );
    }

    void showLongPressMenu(Offset position, BuildContext context) {
      showMenu(
        context: context,
        position: RelativeRect.fromLTRB(
          position.dx, position.dy, position.dx, position.dy, // 根据需要调整位置
        ),
        items: [
          PopupMenuItem(
            value: 'details',
            child: Text('详情'),
          ),
          PopupMenuItem(
            value: 'delete',
            child: Text('删除'),
          ),
        ],
      ).then((value) {
        if (value != null) {
          if (value == 'details') {
            showDetailsDialog(context);
          } else if (value == 'delete') {
            showDeleteConfirmation(context);
          }
        }
      });
    }

    return Clickable(
      backgroundColor: theme.colorScheme.surface,
      onTap: () => jumpWithValue(),
      onLongPress: (Offset position, BuildContext context) => showLongPressMenu(position,context),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if(heightC == 0) ...[
            Expanded(
              child: Container(
                height: heightC != 0 ? heightC : null, // 更明确的默认行为
                margin: const EdgeInsets.all(5), // 外边距
                decoration: BoxDecoration(
                  color: isEven ? Colors.blueAccent[600] : Colors.grey[800],
                  borderRadius: BorderRadius.circular(8),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black,
                      blurRadius: 4,
                      offset: Offset(2, 2),
                    )
                  ],
                ),
                child: Center(
                  child: Text(
                    book.title.split(" ").first,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
            ),
          ] else ...[
            Container(
              height: heightC != 0 ? heightC : null, // 更明确的默认行为
              margin: const EdgeInsets.all(5), // 外边距
              decoration: BoxDecoration(
                color: isEven ? Colors.blueAccent : Colors.grey[800],
                borderRadius: BorderRadius.circular(8),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black,
                    blurRadius: 4,
                    offset: Offset(2, 2),
                  )
                ],
              ),
              child: Center(
                child: Text(
                  book.title.split(" ").first,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
            Text(
              book.title.split(" ").first.length > 8
                  ? '${book.title.split(" ").first.substring(0, 8)}...'
                  : book.title.split(" ").first,
              style: TextStyle(
                color: Colors.white,
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
              overflow: TextOverflow.ellipsis, // 如果使用 maxLines，可以配合使用
              maxLines: 1, // 确保文本不会换行
            ),
            Text("作者：${book.author}",style: TextStyle(fontSize: 8),),
            SizedBox(height: 20,),
          ]
        ],
      )
    );
  }
}
